package com.mdd.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mdd.admin.service.order.IOrderItemService;
import com.mdd.admin.validate.order.OrderItemCreateValidate;
import com.mdd.admin.validate.order.OrderItemUpdateValidate;
import com.mdd.admin.vo.order.OrderItemVo;
import com.mdd.common.entity.order.OrderItem;
import com.mdd.common.mapper.order.OrderItemMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * 订单中所包含的商品实现类
 * @author LikeAdmin
 */
@Service
public class OrderItemServiceImpl implements IOrderItemService {

    @Resource
    OrderItemMapper orderItemMapper;

    /**
     * 订单中所包含的商品列表
     *
     * @author LikeAdmin
     * @param orderId 订单编号
     * @return List<OrderItemVo>
     */
    @Override
    public List<OrderItemVo> list(Integer orderId) {
        LambdaQueryWrapper<OrderItem> orderItemWrapper = new LambdaQueryWrapper<>();
        orderItemWrapper.eq(OrderItem::getOrderId,orderId);
        List<OrderItem> orderItems = orderItemMapper.selectList(orderItemWrapper);

        List<OrderItemVo> list = new LinkedList<>();
        for(OrderItem item : orderItems) {
            OrderItemVo vo = new OrderItemVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }
        return list;
    }

    /**
     * 订单中所包含的商品新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(OrderItemCreateValidate createValidate) {
        OrderItem model = new OrderItem();
        model.setOrderId(createValidate.getOrderId());
        model.setOrderSn(createValidate.getOrderSn());
        model.setProductId(createValidate.getProductId());
        model.setProductPic(createValidate.getProductPic());
        model.setProductName(createValidate.getProductName());
        model.setProductBrand(createValidate.getProductBrand());
        model.setProductSn(createValidate.getProductSn());
        model.setProductPrice(createValidate.getProductPrice());
        model.setProductQuantity(createValidate.getProductQuantity());
        model.setProductSkuId(createValidate.getProductSkuId());
        model.setProductCategoryId(createValidate.getProductCategoryId());
        model.setRealAmount(createValidate.getRealAmount());
        model.setProductAttr(createValidate.getProductAttr());
        orderItemMapper.insert(model);
    }

    /**
     * 订单中所包含的商品编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(OrderItemUpdateValidate updateValidate) {
        OrderItem model = orderItemMapper.selectById(updateValidate.getId());

        Assert.notNull(model, "订单详情数据不存在!");

        model.setId(updateValidate.getId());
        model.setOrderId(updateValidate.getOrderId());
        model.setOrderSn(updateValidate.getOrderSn());
        model.setProductId(updateValidate.getProductId());
        model.setProductPic(updateValidate.getProductPic());
        model.setProductName(updateValidate.getProductName());
        model.setProductBrand(updateValidate.getProductBrand());
        model.setProductSn(updateValidate.getProductSn());
        model.setProductPrice(updateValidate.getProductPrice());
        model.setProductQuantity(updateValidate.getProductQuantity());
        model.setProductSkuId(updateValidate.getProductSkuId());
        model.setProductCategoryId(updateValidate.getProductCategoryId());
        model.setRealAmount(updateValidate.getRealAmount());
        model.setProductAttr(updateValidate.getProductAttr());
        orderItemMapper.updateById(model);
    }

    /**
     * 订单中所包含的商品删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        OrderItem model = orderItemMapper.selectById(id);

        Assert.notNull(model, "订单详情数据不存在!");

        orderItemMapper.deleteById(model);
    }

}
